﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SaleStat.BLL.PM;
using SaleStat.COM.Exceptions;
using SaleStat.COM.Constant;
public partial class PmInfo_Edit : BasePage
{
    #region 初始化
    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                txtProducTypeName.Focus();
                //模式状态
                string flag = Request["flag"].ToString();
                hdnFlag.Value = flag;
                //修改模式
                if ("1".Equals(flag))
                {
                    hdnCpID.Value = Request["CpID"].ToString();
                    getProductsInfo(hdnCpID.Value);
                }

            }

        }
        catch (Exception ex)
        {
            CloseWinWithException(ex);
        }

    }
    #endregion

    #region 确定按钮事件
    /// <summary>
    /// 确定按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgBtnSure_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            //修改模式
            if ("1".Equals(hdnFlag.Value))
            {
                PM_Edit_BLL PmService = new PM_Edit_BLL();
                int intResult = 0;
                //调用bll层更新方法
                intResult = PmService.updateProductInfo(getModifyParam());
                if (intResult <= 0)
                {
                    throw new BusinessException("产品信息修改失败!");
                }
                else
                {
                    Response.Write("<script> alert('产品信息修改成功!'); window.dialogArguments.location.href=window.dialogArguments.location.href;window.focus();window.close();</script>");

                }

            }
            //添加模式
            else
            {
                int intReturn = InsertProuctInfo();
                if (intReturn < 0)
                {

                    throw new BusinessException("产品信息添加失败！");
                }
                else
                {
                    Response.Write("<script> alert('产品信息添加成功！'); window.dialogArguments.location.href=window.dialogArguments.location.href;window.focus();window.close();</script>");

                }

            }

        }

        catch (Exception ex)
        {
            CloseWinWithException(ex);
        }
    }
    #endregion

    #region 自定义方法

    #region 检索产品信息
    /// <summary>
    /// 检索产品信息
    /// </summary>
    /// <param name="strCustomerId"></param>
    public void getProductsInfo(string strCpId)
    {

        PM_Edit_BLL PM_Service = new PM_Edit_BLL();
        DataTable dtProductInfo = new DataTable();
        dtProductInfo = PM_Service.getProductInfo(strCpId);
        if (dtProductInfo == null || dtProductInfo.Rows.Count == 0)
        {
            throw new Exception("产品信息不存在");

        }
        txtCpID.Text = dtProductInfo.Rows[0]["ProductID"].ToString();
        txtProducTypeName.Text = dtProductInfo.Rows[0]["ProducTypeName"].ToString();
        txtGuige.Text = dtProductInfo.Rows[0]["ProducGuiGe"].ToString();
        string strDanwei = dtProductInfo.Rows[0]["ProducUnit"].ToString();
        drpDanWei.SelectedValue = strDanwei;
        txtNote.Text = dtProductInfo.Rows[0]["Note"].ToString();
        hdnRowVer.Value = dtProductInfo.Rows[0]["RowVer"].ToString();
    }
    #endregion

    #region 增加产品信息
    /// <summary>
    /// 增加产品信息
    /// </summary>
    public int InsertProuctInfo()
    {
        PM_Edit_BLL cmService = new PM_Edit_BLL();
        return cmService.insertProductInfo(getAddParam());
    }

    #endregion
    #endregion

    #region 封装要增加的参数
    /// <summary>
    /// ProductID,ProducTypeName,ProducGuiGe,ProducUnit,ProductState,StoreDate,Note,CreateUserID,CreateDate,UpdateUserID,UpdateDate
    /// </summary>
    /// <returns></returns>

    public ArrayList getAddParam()
    {
        ArrayList arrrParamAdd = new ArrayList();
        PM_Edit_BLL pmService = new PM_Edit_BLL();
        string strProuctID = pmService.getMaxProductID();
        string strProducTypeName = txtProducTypeName.Text.Trim();
        string strGuiGe = txtGuige.Text.Trim();
        string strDanWei = drpDanWei.SelectedValue.ToString();
        string strProductState = "1";
        string strNote = txtNote.Text.Trim();
        string strCreateUserID = Session[SessionKeyConstant.SESSION_USERNAME].ToString();

        arrrParamAdd.Add(strProuctID);
        arrrParamAdd.Add(strProducTypeName);
        arrrParamAdd.Add(strGuiGe);
        arrrParamAdd.Add(strDanWei);
        arrrParamAdd.Add(strProductState);
        if (string.IsNullOrEmpty(strNote))
        {
            arrrParamAdd.Add(null);
        }
        else
        {
            arrrParamAdd.Add(strNote);

        }
        arrrParamAdd.Add(strCreateUserID);
        arrrParamAdd.Add(strCreateUserID);
        return arrrParamAdd;


    }


    #region 获取修改参数
    public ArrayList getModifyParam()
    {
        ArrayList arrParam = new ArrayList();
        string strProducTypeName = txtProducTypeName.Text.Trim();
        string strGuiGe = txtGuige.Text.Trim();
        string strDanWei = drpDanWei.SelectedValue.ToString();
        string strNote = txtNote.Text.Trim();
        string strCpID = txtCpID.Text.Trim();

        if (string.IsNullOrEmpty(strProducTypeName))
        {
            arrParam.Add(null);
        }
        else
        {
            arrParam.Add(strProducTypeName);
        }
        if (string.IsNullOrEmpty(strGuiGe))
        {
            arrParam.Add(null);
        }
        else
        {
            arrParam.Add(strGuiGe);
        }

        arrParam.Add(strDanWei);

        if (string.IsNullOrEmpty(strNote))
        {
            arrParam.Add(null);
        }
        else
        {
            arrParam.Add(strNote);
        }
        string strUpdateUserID = Session[SessionKeyConstant.SESSION_USERNAME].ToString();
        arrParam.Add(strUpdateUserID);
        arrParam.Add(strCpID);
        arrParam.Add(hdnRowVer.Value.ToString());


        return arrParam;

    }
    #endregion

    #endregion

}
